package com.leetcode.contest;

import com.leetcode.common.ListNode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2021/11/14 10:48
 */
public class LC5927反转偶数长度组的节点 {

    //TODO 与答案不符
    public static ListNode reverseEvenLengthGroups(ListNode head) {
        int count = 0;
        ListNode cur = head;
        Map<Integer, Integer> edgeMap = new HashMap<>();
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        if (count == 2) return head;
        int k = 1;
        for (int i = 1, j = 0; i < count; i += k, j++) {
            if (j % 2 == 0) edgeMap.put(i, i + k);
            k++;
        }
        System.out.println(edgeMap);
        int i = 0;
        cur = head;
        ListNode prev = null;
        while (cur != null) {
            if (edgeMap.containsKey(i)) {
                //当前位置可以反转，反转数量为val - key
                int cnt = edgeMap.get(i) - i + 1;
                //保存一下原版后继节点
                ListNode successor = getNode(cur, cnt);
                //进行反转
                cur = reverseNode(cur, cnt);
                prev.next = cur;
                ListNode endNode = getEndNode(cur);
                endNode.next = successor;
            }
            prev = cur;
            cur = cur.next;
            i++;
        }
        return head;
    }

    public static void main(String[] args) {
//        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6,
//                new ListNode(7, new ListNode(8, new ListNode(9)))))))));
//        ListNode head = new ListNode(5, new ListNode(2, new ListNode(6, new ListNode(3, new ListNode(9, new ListNode(1,
//                new ListNode(7, new ListNode(3, new ListNode(8, new ListNode(4))))))))));
        ListNode head = new ListNode(0, new ListNode(4, new ListNode(2, new ListNode(1, new ListNode(3)))));
//        reverseEvenLengthGroups(head);
//        System.out.println(getNode(head, 2 + 1).val);
        show(reverseEvenLengthGroups(head));
    }

    private static ListNode reverseNode(ListNode head, int count) {
        ListNode next, cur = head, prev = null;
        while (cur != null && count != 0) {
            next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
            count--;
        }
        return prev;
    }

    private static ListNode getNode(ListNode head, int count) {
        int i = 0;
        ListNode cur = head;
        while (i < count && cur != null) {
            cur = cur.next;
            i++;
        }
        return cur;
    }

    private static ListNode getEndNode(ListNode head) {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        return cur;
    }

    private static void show(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
